#coding=utf-8
from fastapi import Request

import misc
from base.base import AppException
from base.http_base import HttpResp
from common import defines
from utils import auth_util

no_auth_set = {
	"sys_admin:get_self_sys_admin",
	"sys_menu:get_route_menu"
}



async def verify_token(request: Request):
	"""登录状态及权限校验依赖项"""
	if request.url.path.startswith("/game/noauth"):
		return
	
	token = request.headers.get('Authorization', '')
	token_data = auth_util.get_token_data(token)
	request.user_id = token_data.get("user_id", None)
	request.sys_admin_id = token_data.get("sys_admin_id", None)
	request.perms_dict = token_data.get("perms_dict", {})
	request.is_master = token_data.get("is_master", defines.FLAG_NO)
	
	if request.is_master == defines.FLAG_NO and request.url.path.startswith("/admin"):
		auth = request.url.path.replace('/admin/', '').replace('/', ':')
		
		if auth not in no_auth_set and auth not in request.perms_dict:
			raise AppException(HttpResp.NO_PERMISSION)

	